﻿
#region ns
using SerializeProject.Formatters;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
 
#endregion
namespace SerializeProject
{
    class Program
    {
 
        [Format(typeof(JsonFormat))]
        [StorageMode(SerializeProject.Storage.FileSystem)]
        public static GenericFormatter genericFormatter = new GenericFormatter();
        static void Main(string[] args)
        {  
           Stream fileStream = new FileStream("Beverage.json", FileMode.Open, FileAccess.Read);
           Beverage beverage = genericFormatter.Deserialize(fileStream) as Beverage;
        }
        #region binary
        private static void SerializeBeverage(Beverage beverage,Stream fileStream )
        {
            using (fileStream)
            {
                try
                {
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    binaryFormatter.Serialize(fileStream, beverage);
                    Trace.WriteLine("Serialization is successfull!!!");
                }
                catch (FileNotFoundException caught)
                {
                    Trace.WriteLine(string.Format("The message {0}, the source {1}", caught.Message, caught.Source.ToString()));
                }
            }
        }

        private static Beverage DeserializeBeverage(Stream fileStream)
        {
            Beverage beverage;
            using (fileStream)
            {
                try
                {
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    beverage = binaryFormatter.Deserialize(fileStream) as Beverage;
                    Trace.WriteLine("Serialization is successfull!!!");
                    return beverage;
                }
                catch (FileNotFoundException caught)
                {
                    Trace.WriteLine(string.Format("The message {0}, the source {1}", caught.Message, caught.Source.ToString()));
                    return null;
                }
            }
        }
        #endregion
        #region xml
        private static void XmlSerializeBeverage(Beverage beverage, Stream fileStream)
        {
            using (fileStream)
            {
                try
                {
                    XmlSerializer xmlFormatter = new XmlSerializer(typeof(Beverage));
                    xmlFormatter.Serialize(fileStream, beverage);
                    Trace.WriteLine("Serialization is successfull!!!");
                }
                catch (FileNotFoundException caught)
                {
                    Trace.WriteLine(string.Format("The message {0}, the source {1}", caught.Message, caught.Source.ToString()));
                }
            }
        }

        private static Beverage XmlDeserializeBeverage(Stream fileStream)
        {
            Beverage beverage;
            using (fileStream)
            {
                try
                {
                    XmlSerializer xmlFormatter = new XmlSerializer(typeof(Beverage));
                    beverage = xmlFormatter.Deserialize(fileStream) as Beverage;
                    Trace.WriteLine("Serialization is successfull!!!");
                    return beverage;
                }
                catch (FileNotFoundException caught)
                {
                    Trace.WriteLine(string.Format("The message {0}, the source {1}", caught.Message, caught.Source.ToString()));
                    return null;
                }
            }
        }
        #endregion
        #region soap 
        private static void SoapSerializeBeverage(Beverage beverage, Stream fileStream)
        {
            using (fileStream)
            {
                try
                {
                    SoapFormatter soapFormatter = new SoapFormatter();
                    soapFormatter.Serialize(fileStream, beverage);
                    Trace.WriteLine("Serialization is successfull!!!");
                }
                catch (FileNotFoundException caught)
                {
                    Trace.WriteLine(string.Format("The message {0}, the source {1}", caught.Message, caught.Source.ToString()));
                }
            }
        }

        private static Beverage SoapDeserializeBeverage(Stream fileStream)
        {
            Beverage beverage;
            using (fileStream)
            {
                try
                {
                    SoapFormatter soapFormatter = new SoapFormatter();
                    beverage = soapFormatter.Deserialize(fileStream) as Beverage;
                    Trace.WriteLine("Serialization is successfull!!!");
                    return beverage;
                }
                catch (FileNotFoundException caught)
                {
                    Trace.WriteLine(string.Format("The message {0}, the source {1}", caught.Message, caught.Source.ToString()));
                    return null;
                }
            }
        }
        #endregion 

    }//Class
}//Namespace
